(function() {
    var util = require("common:widget/ui/libs/util.js").util,
        head = require('home:widget/ui/whitehead/whitehead.js');
    var loginUtil =  {
        head : null,
        init : function() {
            var me = this;
            //调整页面大小
            var windowHeight = $(window).height();
            $('.section').height(windowHeight);
            if(windowHeight < 885) {
                $('.login_form').css('top',100);
                $('#footer').css('top',540);
                $('.section').height('885');
            } else {
                var height = $(window).height()/2-235;
                $('.login_form').css('top',height);
            }
            me.head = new head($('.section') , 'white');
            if(util.readCookie("zhenrongbao_user_name")){
                $('#username').val(util.readCookie("zhenrongbao_user_name"));
            }
            this.eventCenter();
        },
        eventCenter : function() {
            var me = this;
            $('[placeholder]').focus(function() {
                var input = $(this);
                if (input.val() == input.attr('placeholder')) {
                    input.val('');
                    input.removeClass('placeholder');
                }
            }).blur(function() {
                    var input = $(this);
                    if (input.val() == '' || input.val() == input.attr('placeholder')) {
                        input.addClass('placeholder');
                        input.val(input.attr('placeholder'));
                    }
                }).blur();
            $('[placeholder]').parents('form').submit(function() {
                $(this).find('[placeholder]').each(function() {
                    var input = $(this);
                    if (input.val() == input.attr('placeholder')) {
                        input.val('');
                    }
                })
            });
            $('.input_text').click(function(event) {
                $(event.target).find('input').focus();
            });
            $('#login_button').click(function () {
                me.checkForm();
            });

            $(document).on('keydown',function(e){
                if(e.keyCode == 13 &&  $('#username').val() && $('#password').val()){
                    $('#login_button').trigger("click");
                }
            });
        },
        checkForm: function() {
            $('.info_text').text('');
            var username = $('#username').val();
            if(username.length < 11) {
                $('#username').parent().next().text('请输入11位手机号。');
                return;
            }
            var password = $('#password').val();
            if(password.length < 6 || password.length > 16) {
                $('#password').parent().next().text('请输入6~16位密码。');
                return;
            }
            if($("#rember_un:checked").length){
                util.setCookie("zhenrongbao_user_name="+username);
            }else{
                util.delCookie("zhenrongbao_user_name");
            }
            $.post('/account/loginuser',{'user_name':username,'passwd':password}, function(data) {
                if(data && data.error_no == 0) {
                    var param = util.getUrlParas(),
                        from = param['from']?param['from']:'/user/wallet';
                    location.href= from;
                } else {
                    if(data.error_no == 10201 || data.error_no == 10202) {
                        $('#username').parent().next().text(data.error_message);
                    } else if(data.error_no == 10203) {
                        $('#password').parent().next().text(data.error_message);
                    } else {
                        alert(data.error_message);
                    }
                }
            },'json');
        }
    }
    loginUtil.init();
})();
